﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BTL
{
    public partial class NhaCC : Form
    {
        VO.nhacc_vo _ncc;
        BAL.NhaCCBAL _nccbal;
        public NhaCC()
        {
            InitializeComponent();
            comboBox1.Text = "Mã nhà cung cấp";
            
        }
        private void data()
        {
            _ncc = new VO.nhacc_vo();
            _ncc.Diachi = box_diachi.Text;
            _ncc.Sdt = Convert.ToInt32(box_sdt.Text);
            _ncc.Tenncc = box_tenncc.Text;
            //_ncc.Mancc = Convert.ToInt32(box_mancc.Text);
        }
        private void databind()
        {
            box_diachi.DataBindings.Clear();
            box_diachi.DataBindings.Add("text",dataGridViewnhacc.DataSource,"DiaChi");
            box_mancc.DataBindings.Clear();
            box_mancc.DataBindings.Add("text", dataGridViewnhacc.DataSource, "MaNCC");
            box_tenncc.DataBindings.Clear();
            box_tenncc.DataBindings.Add("text", dataGridViewnhacc.DataSource, "TenNCC");
            box_sdt.DataBindings.Clear();
            box_sdt.DataBindings.Add("text", dataGridViewnhacc.DataSource, "SDT");
        }
        private void bt_them_Click(object sender, EventArgs e)
        {
            data();
            try
            {
                _nccbal = new BAL.NhaCCBAL();
                dataGridViewnhacc.DataSource = _nccbal.NhaCC_them(_ncc);
                bt_hienthi_Click(sender,e);
                MessageBox.Show("Thành công", "Đã thêm nhà cung cấp", MessageBoxButtons.OK);
                NhaCC_Load(sender, e);
            }
            catch
            {
                MessageBox.Show("Lỗi","Thêm không thành công",MessageBoxButtons.OK );
            }
        }

        private void bt_hienthi_Click(object sender, EventArgs e)
        {
            try
            {
                _nccbal = new BAL.NhaCCBAL();
                dataGridViewnhacc.DataSource = _nccbal.NhaCC_xem();
            }
            catch
            {
                MessageBox.Show("Lỗi", "không kết nối được", MessageBoxButtons.OK);
            }
            
        }

        private void bt_sua_Click(object sender, EventArgs e)
        {
            data();
            try
            {
                _nccbal = new BAL.NhaCCBAL();
                dataGridViewnhacc.DataSource = _nccbal.NhaCC_Sua(_ncc);
                bt_hienthi_Click(sender, e);
                MessageBox.Show("Thành công", "Đã sửa nhà cung cấp", MessageBoxButtons.OK);
                NhaCC_Load(sender, e);
            }
            catch
            {
                MessageBox.Show("Lỗi", "sửa không thành công", MessageBoxButtons.OK);
            }
        }

        private void bt_xoa_Click(object sender, EventArgs e)
        {
            data();
            try
            {
                if(MessageBox.Show("Bạn có muốn xóa không","Xóa?",MessageBoxButtons.OKCancel)==DialogResult.OK)
                { 
                _ncc = new VO.nhacc_vo();
                _ncc.Mancc =Convert.ToInt32( box_mancc.Text);
                _nccbal = new BAL.NhaCCBAL();
                dataGridViewnhacc.DataSource = _nccbal.NhaCC_xoa(_ncc);
                bt_hienthi_Click(sender, e);
                MessageBox.Show("Thành công", "Đã xóa nhà cung cấp", MessageBoxButtons.OK);
                NhaCC_Load(sender, e);
                }
            }
            catch
            {
                MessageBox.Show("Lỗi", "nhập mã nhà cung cấp để xóa", MessageBoxButtons.OK);
            }
        }

        private void bt_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_timkiem_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Mã nhà cung cấp")
            {
                SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from HoaDon where MaHD like '%" + textBox1.Text.Trim() + "%'";
                da.Fill(dt);
                dataGridViewnhacc.DataSource = dt;
                conn.Close();
            }
            if (comboBox1.Text == "Tên nhà cung cấp")
            {
                SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                cmd.Connection = conn;
                conn.Open();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from HoaDon where MaDG like '%" + textBox1.Text.Trim() + "%'";
                da.Fill(dt);
                dataGridViewnhacc.DataSource = dt;
                conn.Close();
            }
        }

        private void NhaCC_Load(object sender, EventArgs e)
        {
            bt_hienthi_Click(sender,e);
            databind();
        }
    }
}
